package org.ehe.business.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.ehe.business.project.domain.ProjectDifficultyConfigEntity;
import org.ehe.business.project.mapper.ProjectDifficultyConfigMapper;
import org.ehe.business.project.service.ProjectDifficultyConfigService;
import org.ehe.common.core.constant.HttpStatus;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author :zhangnn
 * @className :ProjectDifficultyConfigServiceImpl
 * @description: TODO
 * @date 2025-07-17 16:51:16
 */
@Service
public class ProjectDifficultyConfigServiceImpl extends ServiceImpl<ProjectDifficultyConfigMapper, ProjectDifficultyConfigEntity> implements ProjectDifficultyConfigService {

    /**
     * 根据ID获取项目信息
     * @param difficultyId
     * @return
     */
    @Override
    public ProjectDifficultyConfigEntity getByDifficulty(String difficultyId) {
        return this.baseMapper.selectById(difficultyId);
    }

    /**
     * 获取难度列表
     * @return
     */
    @Override
    public List<ProjectDifficultyConfigEntity> queryList() {
        return this.baseMapper.selectList(new QueryWrapper<ProjectDifficultyConfigEntity>().eq("del_flag",0));
    }

    /**
     * 获取困难度列表
     * @param configEntity
     * @return
     */
    @Override
    public List<ProjectDifficultyConfigEntity> queryConfigList(ProjectDifficultyConfigEntity configEntity) {
        List<ProjectDifficultyConfigEntity> configEntities = this.baseMapper.selectList(
            new LambdaQueryWrapper<ProjectDifficultyConfigEntity>().eq(StringUtils.isNotBlank(configEntity.getName()),
                ProjectDifficultyConfigEntity::getName,configEntity.getName())
                .orderByAsc(ProjectDifficultyConfigEntity::getDays)
        );
        return configEntities;
    }

    /**
     *
     * @param configEntity
     */
    @Override
    public String saveDifficultConfig(ProjectDifficultyConfigEntity configEntity) {
        ProjectDifficultyConfigEntity projectDifficultyConfigEntity = this.baseMapper.selectOne(new LambdaQueryWrapper<ProjectDifficultyConfigEntity>()
            .eq(ProjectDifficultyConfigEntity::getName, configEntity.getName()));
        if(Objects.nonNull(projectDifficultyConfigEntity)){
            return "["+configEntity.getName()+"]已存在，请勿重复配置";
        }

        configEntity.setCreateBy(LoginHelper.getUsername());
        configEntity.setTenantId(LoginHelper.getTenantId());
        int insert = this.baseMapper.insert(configEntity);
        return insert+"";

    }

    /**
     * 修改
     * @param configEntity
     * @return
     */
    @Override
    public String updateDifficultConfig(ProjectDifficultyConfigEntity configEntity) {
        configEntity.setUpdateBy(LoginHelper.getUsername());
        ProjectDifficultyConfigEntity projectDifficultyConfigEntity = this.baseMapper.selectOne(
            new LambdaQueryWrapper<ProjectDifficultyConfigEntity>()
            .eq(ProjectDifficultyConfigEntity::getName, configEntity.getName()));
        if(Objects.nonNull(projectDifficultyConfigEntity)){
            return String.valueOf(HttpStatus.WARN);
        }
        this.baseMapper.updateById(configEntity);
        return "修改项目难度配置成功";
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteConfig(Long id) {
        return this.baseMapper.deleteDifficultyConfigById(id);
    }
}
